﻿@page "/code"
@rendermode InteractiveServer

@using Newtonsoft.Json
@using IdentityModel.Client
@inject HttpClient httpClient
@inject NavigationManager nav

<PageTitle>授权码模式</PageTitle>

@* <DxButton Click="GetOpenIdConfig">获取配置</DxButton> *@
@* <p>@OpenIdConfig</p> *@
@* *@
@* <div> *@
@*     客户端ID：<DxTextBox @bind-Text="@ClientId"/> *@
@*     客户端秘钥：<DxTextBox @bind-Text="@ClientSecret"/> *@
@*     ApiScope：<DxTextBox @bind-Text="@ApiScope"/> *@
@*     用户名：<DxTextBox @bind-Text="@UserName"/> *@
@*     密码：<DxTextBox @bind-Text="@Pwd"/> *@
@* </div> *@
@* <DxButton Click="@GetToken">认证</DxButton> *@
@* <p style="width: 400px">@Token</p> *@
@* *@
@* <DxTextBox @bind-Text="@TokenString"/> *@
@* <DxButton Click="@GetApiData">获取Api数据</DxButton> *@
@* <p style="width: 400px">@ApiData</p> *@
@* *@
@* @code *@
@* { *@
@*     private string? OpenIdConfig = String.Empty; *@
@* *@
@*     private async Task GetOpenIdConfig() *@
@*     { *@
@*         string url = $"{nav.BaseUri}.well-known/openid-configuration"; *@
@*         var result = await httpClient.GetFromJsonAsync<OpenIdConfigModel>(url); *@
@*         OpenIdConfig = JsonConvert.SerializeObject(result, Formatting.Indented); *@
@*     } *@
@* *@
@*     private string? Token = String.Empty; *@
@*     private string? ClientId = "password_client"; *@
@*     private string? ClientSecret = "511536EF-F270-4058-80CA-1C89C192F69A"; *@
@*     private string? ApiScope = "pwd_scope1"; *@
@*     private string? UserName = "unitysir"; *@
@*     private string? Pwd = "123456"; *@
@*     private async Task GetToken() *@
@*     { *@
@*         var docu = await httpClient.GetDiscoveryDocumentAsync(nav.BaseUri); *@
@* *@
@*         var resp = await httpClient.RequestPasswordTokenAsync(new PasswordTokenRequest() *@
@*         { *@
@*             Address = docu.TokenEndpoint, *@
@*             ClientId = ClientId, *@
@*             ClientSecret = ClientSecret, *@
@*             Scope = ApiScope, *@
@*             UserName=UserName, *@
@*             Password=Pwd *@
@*         }); *@
@* *@
@*         Token = JsonConvert.SerializeObject(resp.Json, Formatting.Indented); *@
@*     } *@
@* *@
@*     private string? TokenString = String.Empty; *@
@*     private string? ApiData = String.Empty; *@
@* *@
@*     private async Task GetApiData() *@
@*     { *@
@*         string url = "http://localhost:5096/WeatherForecast/GetUserClaims"; *@
@*         httpClient.SetBearerToken(TokenString); *@
@*         var response = await httpClient.GetAsync(url); *@
@*         if (response.IsSuccessStatusCode) *@
@*         { *@
@*             ApiData = await response.Content.ReadAsStringAsync(); *@
@*         } *@
@*         else *@
@*         { *@
@*             ApiData = $"{response.StatusCode}"; *@
@*         } *@
@*     } *@
@* } *@